<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>交互式的图片对比控件</title>
    <style>
        .image-slider{
            position: relative;
            display: inline-block;
        }
        .image-slider>div{
            position: absolute;
            top: 0;
            left: 0;
            width: 50%;
            overflow: hidden;
        }
        .image-slider img{
            display: block;
            user-select: none;
        }
    </style>
</head>

<body>
    <div class="image-slider">
        <img src="./img/1.png" alt="">
        <img src="./img/6.png" alt="">
        <input type="range">
    </div>

    <script>
        $$('.image-slider').forEach(function (slider) {
            var div = document.createElement('div');
            var img = document.querySelector('img');
            slider.insertBefore(div, img);
            div.appendChild(img);

            var range = document.createElement('input');
            range.type = 'range';
            range.oninput = function(){
                div.style.width = this.value + '%';
            };
            slider.appendChild(range);
        });
    </script>
</body>

</html>